﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows;
using HtmlAgilityPack;
using NthDownload.Common.ViewModels;
using NthDownload.Contracts;
using System.Linq;

namespace NthDownload.DownloadPlugins.Http.ViewModels
{
    public class HttpDownloadAddViewModel : ViewModelScreenBase
    {
        #region Class Members

        private string _path;
        private string _destinationPath;

        #endregion Class Members

        #region Constructors

        public HttpDownloadAddViewModel()
        {
            TryGetPathFromClipboard();
            DestinationPath = Settings.DefaultDestinationPath;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Source of the download
        /// </summary>
        public string Path 
        {
            get
            {
                return _path;
            }
            set
            {
                _path = value;
                NotifyOfPropertyChange(() => Path);
            }
        }

        /// <summary>
        /// Destination of the download
        /// </summary>
        public string DestinationPath
        {
            get
            {
                return _destinationPath;
            }
            set
            {
                _destinationPath = value;
                NotifyOfPropertyChange(() => DestinationPath);
            }
        }

        /// <summary>
        /// Current Settings
        /// </summary>
        [Import(AllowRecomposition = true)]
        public HttpSettings Settings { get; set; }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Closes the dialog with a save result
        /// </summary>
        public void Ok()
        {
            if (!BindingGroupValidator.ValidateBindingExpressionsWithUpdate())
            {
                return;
            }

            Result = ViewModelResults.Ok;
            TryClose();
        }

        /// <summary>
        /// Closes the dialog with a save result
        /// </summary>
        public void Cancel()
        {
            
            Result = ViewModelResults.Cancel;
            TryClose();
        }

        /// <summary>
        /// Changes the destination path
        /// </summary>
        public void ChangeDestination()
        {
            System.Windows.Forms.FolderBrowserDialog dlgDest = new System.Windows.Forms.FolderBrowserDialog();
            if (dlgDest.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            DirectoryInfo diDest = new DirectoryInfo(dlgDest.SelectedPath);

            if (!diDest.Exists)
            {
                return;
            }

            DestinationPath = diDest.FullName;
        }

        #endregion Methods

        #region Private Methods

        /// <summary>
        /// Tries to get a path from the current text in the clipboard
        /// </summary>
        private void TryGetPathFromClipboard()
        {
            Uri uri;
            if (!Uri.TryCreate(Clipboard.GetText(), UriKind.Absolute, out uri)) return;
            if (uri.Scheme != "http" && uri.Scheme != "https") return;

            Path = uri.OriginalString;
        }

        #endregion Private Methods
    }
}
